/*
 * Copyright 2012 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.android.calllog;

import java.util.ArrayList;
import java.util.Map;

import nl.biz_e.android.calllog.dao.CalendarDao;
import nl.biz_e.android.calllog.util.CallLogPrefs;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;

/**
 * An implementation of {@link PreferenceActivity} to enable the user to manipulate preferences.
 * <p>
 * Still uses the old (deprecated) preference API calls, should be modernized. 
 * </p>
 * :TODO implement modern Preference API
 *  
 * @author Joost A. Bloemsma
 */
public class CallLogPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	private Preference prefInterval;
	private ListPreference prefCalCalendar;
	
	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getPreferenceManager().setSharedPreferencesName(CallLogPrefs.PREFS_NAME);
		addPreferencesFromResource(R.xml.settings);
		SharedPreferences prefs = getApplicationContext().getSharedPreferences(CallLogPrefs.PREFS_NAME, Context.MODE_PRIVATE);
		prefs.registerOnSharedPreferenceChangeListener(this);
		prefInterval = findPreference(getString(R.string.prefs_service_interval_key));
		prefCalCalendar = (ListPreference)findPreference(getString(R.string.prefs_calendar_calendar_key));
		initPrefs();
	}

	/**
	 * {@inheritDoc}
	 */
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals(getString(R.string.prefs_service_interval_key))) {
			updateIntervalSummary();
		}
		if (key.equals(getString(R.string.prefs_calendar_calendar_key))) {
			updateCalendarSummary();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		SharedPreferences prefs = getApplicationContext().getSharedPreferences(CallLogPrefs.PREFS_NAME, Context.MODE_PRIVATE);
		prefs.unregisterOnSharedPreferenceChangeListener(this);
	}
	
	private void updateIntervalSummary() {
		prefInterval.setSummary(CallLogPrefs.getServiceIntervalText());
	}

	private void updateCalendarSummary() {
		prefCalCalendar.setSummary(CallLogPrefs.getCurrentCalendarName());
	}
	
	private void initPrefs() {
		updateIntervalSummary();
		initCalendarList();
		updateCalendarSummary();
	}
	
	private void initCalendarList() {
		Map<Long, String> calendars =  CalendarDao.getInstance().getCalendars();
		ArrayList<String> idList = new ArrayList<String>();
		ArrayList<String> nameList = new ArrayList<String>();
		String[] ids = new String[calendars.size()];
		String[] names = new String[calendars.size()];
		for (Long id : calendars.keySet()) {
			idList.add(id.toString());
			nameList.add(calendars.get(id));
		}
		idList.toArray(ids);
		nameList.toArray(names);
		prefCalCalendar.setEntries(names);
		prefCalCalendar.setEntryValues(ids);
	}
}
